#pragma once

#include"IMesh.h"



struct SVertex
{
	XMFLOAT3 pos;
};

static const D3D11_INPUT_ELEMENT_DESC g_vertexLayout[] = 
{
	 {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, 
};

static const UINT g_numVertexLayoutElements = ARRAYSIZE(g_vertexLayout);


class pgMesh : public IMesh
{
public:
	pgMesh();
	 ~pgMesh();

	 ID3D11Buffer *GetVertexBuffer() {return m_pVertexBuffer;}
	 ID3D11Buffer *GetIndexBuffer() {return m_pIndexBuffer;}
	 int GetVertexCount() {return m_vertexCount;}
	 int GetIndexCount() {return m_indexCount;}
	 void SetVertices(SVertex *pVerts, int vertexCount);
	 void SetIndices(WORD *pIndices, int indexCount);

	 // TODO:
	 // Create buffers should check if the buffers have changed since the last creation
	 // and only create them if this has happened
	 void CreateBuffers();

	 SVertex *GetVertices(int &vertexCount)
	 {
		 vertexCount = m_vertexCount;
		 return m_apVerts;
	 }
	 SVertex *CopyVertices(int &vertexCount)
	 {
		 SVertex *pCopy = new NEW_MEM_LEAK SVertex[m_vertexCount];

		 for (int i = 0; i < m_vertexCount; ++i)
		 {
			 pCopy[i].pos = m_apVerts[i].pos;
		 }

		 vertexCount = m_vertexCount;
		 return pCopy;
	 }
	 WORD *GetIndices(int &indexCount)
	 {
		 indexCount = m_indexCount;
		 return m_apIndices;
	 }
	 WORD *CopyIndices(int &indexCount)
	 {
		 WORD *pCopy = new NEW_MEM_LEAK WORD[m_indexCount];

		 for (int i = 0; i < m_indexCount; ++i)
		 {
			 pCopy[i] = m_apIndices[i];
		 }

		 indexCount = m_indexCount;
		 return pCopy;
	 }

private:
	
	// Takes a line of three digits from the file and sets
	// the vertex x, y and z coordinates according to it
	void SetVertexFromLine(int index, std::string line);
	// Reads the indizes from the file and creates an array of
	// integers with them 
	void ReadIndizes();
	// Reads the vertex information from the file and creates an array 
	// of SVertex structures to hold the information
	void ReadVerts();
	
	SVertex *m_apVerts;
	WORD *m_apIndices;
	ID3D11Buffer *m_pVertexBuffer;
	ID3D11Buffer *m_pIndexBuffer;


	int m_vertexCount;
	int m_indexCount;
};